<?php

	// This function parses the input, validates credentials and returns an array
	// with requested device statuses. This array is then used to tell the Tellstick
	// object how to set the devices.

	function parse_input()
	{
		// Get username and password from request
		$username      = $_REQUEST['u'];
		$password_hash = $_REQUEST['p'];
        
        foreach($_REQUEST as $key => $value)
        {
            echo($key . '=' . $value . '; '); 
        }
        
		// Validate username and password
		validate_user($username, $password_hash);

		// Convert comma-separated list of devices into separate arrays for on and off
		$on               = $_REQUEST['on']   != '' ? explode(',', $_REQUEST['on'])  : array();
		$off              = $_REQUEST['off']  != '' ? explode(',', $_REQUEST['off']) : array();

		// See if we should include a list of devices in the response
		$list             = $_REQUEST['list'] == 'true' ? true : false;

		// Create an array of devices that should be excluded from switching and response
		$excluded_devices = explode(',', str_replace(' ', '', EXCLUDE_DEVICES));

		// Init requested device status array
		$device_list = array();

		// Loop through devices requested to be switched on and store requested status in device status array
		foreach($on as $device)
			if(is_numeric($device) && !in_array($device, $excluded_devices))
				$device_list[$device] = 1;

		// Loop through devices requested to be switched off and store requested status in device status array
		foreach($off as $device)
			if(is_numeric($device) && !in_array($device, $excluded_devices))
				$device_list[$device] = 0;

		// Return the requested device status array
		return array('device_list' => $device_list, 'list' => $list);
	}


	// This function compares the username and password from the request against
	// the values in config.php. Aborts script and prints an error if they don't match.
	
	function validate_user($username, $password)
	{
		if($username != USERNAME || $password != md5(PASSWORD))
			die(response('AUTH_FAIL'));
	}


	// This function prints a response in the desired format. Currently only JSON
	// is supported. If a device list is requested it includes this in the response.

	function response($result, $devices = array(), $list = false)
	{
		$response = array();
		$excluded_devices = explode(',', str_replace(' ', '', EXCLUDE_DEVICES));

		switch($result) :

			case 'AUTH_FAIL' :
				$response['success'] = false;
				$response['reason']  = 'Authorization failed';
				break;

			case 'FAIL' :
				$response['success'] = false;
				$response['reason']  = 'Devices were not set correctly';
				break;

			case 'OK' :
				$response['success'] = true;
				break;

		endswitch;

		if($list) :
			$response['devices'] = array();
			
			foreach($devices as $device)
				if(!in_array($device['id'], $excluded_devices))
					$response['devices'][] = $device;
		endif;

		return encode_response($response);
	}


	// Encodes an object to the format specified in config.php

	function encode_response($response)
	{
		switch(RESPONSE_FORMAT) :

			case 'json' :
				$encoded_response = json_encode($response);
				break;

		endswitch;

		return $encoded_response;
	}

?>
